﻿using System.Text;
using FCTC_Web.Models;
using DAL;
using FCTC_Web.Utilities;
using System.Globalization;

namespace System.Web.Mvc
{
    public static class HtmlExtensions
    {
        public static HtmlString GetHtmlString(string s)
        {
            return new HtmlString(s);
        }
        public static HtmlString Pager<T>(this HtmlHelper helper, Page<T> page, string urlPrefix) where T : class
        {
            // return new HtmlString(String.Format("<label id='{0}'>{1}</label>", "aaa", "a"));
            if (page.List == null) return null;
            var currentIndex = page.CurrentPage;
            var lastPage = page.LastPage;
            StringBuilder sb1 = new StringBuilder();
            string marks = string.Empty;
            if (urlPrefix.Contains(@"?"))
                marks = @"&pageId=";
            else
                marks = @"?pageId=";



            sb1.AppendLine("<center class='MaintainsPages'>");
            sb1.AppendLine("<table>");
            sb1.AppendLine("<tr>");
            if (page.CurrentPage > 1)
                sb1.AppendLine(String.Format("<td class='fanye_0'><a href=\"{0}" + marks + "{1}\">【首页】</a></td>", urlPrefix, 1));
            int idxPre = page.CurrentPage >= 2 ? currentIndex - 1 : currentIndex;
            sb1.AppendLine(String.Format("<td class='fanye_7'><a href=\"{0}" + marks + "{1}\">上一页</a></td>", urlPrefix, idxPre));
            //if (page.CurrentPage >= 2)
            //    sb1.AppendLine(String.Format("<li><a href=\"{0}" + marks + "{1}\"><</a><li>", urlPrefix, currentIndex - 1));

            //if (currentPage - currentPageSize >= 0)
            //    sb1.AppendLine(String.Format("<a href=\"{0}/{1}\">...</a>", urlPrefix, (currentPage - currentPageSize) + 1));

            for (int i = 0; i < page.LastPage; i++)
            {
                if (currentIndex == i + 1) sb1.AppendLine(String.Format("<td><a href=\"{0}" + marks + "{1}\" style='color:red;font-size:14px;'>【{1}】</a></td>", urlPrefix, i + 1));
                else sb1.AppendLine(String.Format("<td><a href=\"{0}" + marks + "{1}\">【{1}】</a></td>", urlPrefix, i + 1));
            }

            //if (currentPage + currentPageSize <= (Math.Round((totalRecords / 10) + 0.5) - 1))
            //    sb1.AppendLine(String.Format("<a href=\"{0}/{1}\">...</a>", urlPrefix, (currentPage + currentPageSize) + 1));

            //if (currentIndex < lastPage)
            //    sb1.AppendLine(String.Format("<li><a href=\"{0}" + marks + "{1}\">></a><li>", urlPrefix, currentIndex + 1));
            int idxAft = page.CurrentPage < lastPage ? currentIndex + 1 : currentIndex;
            sb1.AppendLine(String.Format("<td class='fanye_8'><a href=\"{0}" + marks + "{1}\">下一页</a></td>", urlPrefix, idxAft));
            if (currentIndex < lastPage)
                sb1.AppendLine(String.Format("<td class='fanye_0'><a href=\"{0}" + marks + "{1}\">【末页】</a></td>", urlPrefix, lastPage));
            sb1.AppendLine("</tr>");
            sb1.AppendLine("</table>");


            //sb1.AppendLine("<table>");
            //sb1.AppendLine("</ul>");
            sb1.AppendLine("</center>");

            return new HtmlString(sb1.ToString());
        }

        public static HtmlString Pager<T>(this HtmlHelper helper, Page<T> page, string urlPrefix,string Flag) where T : class
        {
            // return new HtmlString(String.Format("<label id='{0}'>{1}</label>", "aaa", "a"));
            if (page.List == null) return null;
            var currentIndex = page.CurrentPage;
            var lastPage = page.LastPage;
            StringBuilder sb1 = new StringBuilder();
            string marks = string.Empty;
            if (urlPrefix.Contains(@"?"))
                marks = @"&pageId=";
            else
                marks = @"?pageId=";

         

            sb1.AppendLine("<center class='MaintainsPages'>");
            sb1.AppendLine("<table  cellspacing='5px;'>");
            sb1.AppendLine("<tr>");
            if (page.CurrentPage > 1)
                sb1.AppendLine(String.Format("<td><a href=\"{0}" + marks + "{1}\">首页</a></td>", urlPrefix, 1));
            int idxPre = page.CurrentPage >= 2 ? currentIndex - 1 : currentIndex;
            sb1.AppendLine(String.Format("<td><a href=\"{0}" + marks + "{1}\">上一页</a></td>", urlPrefix, idxPre));
            //if (page.CurrentPage >= 2)
            //    sb1.AppendLine(String.Format("<li><a href=\"{0}" + marks + "{1}\"><</a><li>", urlPrefix, currentIndex - 1));

            //if (currentPage - currentPageSize >= 0)
            //    sb1.AppendLine(String.Format("<a href=\"{0}/{1}\">...</a>", urlPrefix, (currentPage - currentPageSize) + 1));

            for (int i = 0; i < page.LastPage; i++)
            {
                sb1.AppendLine(String.Format("<td><a href=\"{0}" + marks + "{1}\">{1}</a></td>", urlPrefix, i + 1));
            }

            //if (currentPage + currentPageSize <= (Math.Round((totalRecords / 10) + 0.5) - 1))
            //    sb1.AppendLine(String.Format("<a href=\"{0}/{1}\">...</a>", urlPrefix, (currentPage + currentPageSize) + 1));

            //if (currentIndex < lastPage)
            //    sb1.AppendLine(String.Format("<li><a href=\"{0}" + marks + "{1}\">></a><li>", urlPrefix, currentIndex + 1));
            int idxAft = page.CurrentPage < lastPage ? currentIndex + 1 : currentIndex;
            sb1.AppendLine(String.Format("<td><a href=\"{0}" + marks + "{1}\">下一页</a></td>", urlPrefix, idxAft));
            if (currentIndex < lastPage)
                sb1.AppendLine(String.Format("<td><a href=\"{0}" + marks + "{1}\">末页</a></td>", urlPrefix, lastPage));

            sb1.AppendLine("</tr>");
            sb1.AppendLine("</table>");
             
            sb1.AppendLine("</center>");

            return new HtmlString(sb1.ToString());
        }
    }
}